package com.lotter.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lotter.common.context.YcContext;
import com.lotter.common.enumobject.UserAccountStatusEunm;
import com.lotter.common.enumobject.UserAccountTypeEunm;
import com.lotter.common.util.DateUtil;
import com.lotter.context.GlobConst;
import com.lotter.dao.HourseNewDao;
import com.lotter.dao.LotterOrderDao;
import com.lotter.dao.UserMapper;
import com.lotter.dao.WinpayflowDao;
import com.lotter.model.hoursenew.HoursenewsListVo;
import com.lotter.model.lotterorder.LotterStat;
import com.lotter.model.user.HourseUserVo;
import com.lotter.model.user.UserHourseApplyVo;
import com.lotter.model.user.UserListVo;
import com.lotter.model.winpayflow.WinPayFlowVo;

@Service
public class UserService {

	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private LotterOrderDao lotterOrderDao;
	
	@Autowired
	private HourseNewDao hourseNewDao;
	@Autowired
	private WinpayflowDao winpayflowDao;
	
	public Map<String,Object> usrelistPage(String mobile, Integer page,Integer rows,Map<String,Object> result) {
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		List<UserListVo> list = userMapper.getUserListVos(mobile,UserAccountTypeEunm.SUBSCRIBER.getCode(),UserAccountStatusEunm.NORMAL.getCode(), start, rows);
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		int total = userMapper.getUserListVoCount(mobile,UserAccountTypeEunm.SUBSCRIBER.getCode(),UserAccountStatusEunm.NORMAL.getCode());
		result.put(GlobConst.page_total,total);//total键 存放总记录数，必须的  
        return result;
    }
	
	
	public Map<String,Object> hourselistPage(String mobile, Integer page,Integer rows,Map<String,Object> result) {
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		List<HourseUserVo> list = userMapper.getHourseUserVo(mobile,UserAccountTypeEunm.PUBLISHER.getCode(),UserAccountStatusEunm.NORMAL.getCode(), start, rows);
		List<Long> userIdsArr = new ArrayList<Long>();
		for(HourseUserVo vo: list){
			userIdsArr.add(vo.getId());
		}
		List<LotterStat> statList = lotterOrderDao.getLotterStatList(userIdsArr);//
		
		for(HourseUserVo vo: list){
			for(LotterStat obj2:statList) {
				if(obj2.getUid()==vo.getId()) {
					vo.setRedrate((float)obj2.getSumcount()/(float)vo.getBallotnum());
					break;
				}
			}
		}
		int total = userMapper.getUserListVoCount(mobile,UserAccountTypeEunm.PUBLISHER.getCode(),UserAccountStatusEunm.NORMAL.getCode());
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		result.put(GlobConst.page_total,total );//total键 存放总记录数，必须的  
        return result;
    }
	
	public Map<String,Object> hoursercheckPage(String mobile,Integer page,Integer rows,Map<String,Object> result) {
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		List<UserHourseApplyVo> list = userMapper.getUserapplyhourse(mobile,start, rows);
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		result.put(GlobConst.page_total, userMapper.getUserapplyhourseCount(mobile));//total键 存放总记录数，必须的  
        return result;
    }
	
	public Map<String,Object> winPayFlowVosPage(String startdatestr, String enddatestr,
			Integer page,Integer rows,Map<String,Object> result) throws Exception{
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		Date startdate=null,enddate=null;
		if(StringUtils.isNotBlank(startdatestr)) {
			startdate = DateUtil.stringToDate(startdatestr, YcContext.DATE_STR_PATTERN);
		}
		if(StringUtils.isNotBlank(enddatestr)) {
			enddate = DateUtil.stringToDate(enddatestr, YcContext.DATE_STR_PATTERN);
		}
		List<WinPayFlowVo> list = winpayflowDao.getWinPayFlowVos(startdate,enddate,start, rows);
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		result.put(GlobConst.page_total, winpayflowDao.getWinPayFlowVoCount(startdate,enddate));//total键 存放总记录数，必须的  
        return result;
    }
	
	public void updateuserhourse(Long id,Integer utype) {
		userMapper.updateHourse(id, utype);
	}
	
	public Map<String,Object> hoursenewsPage(Long uid, Integer page,Integer rows,Map<String,Object> result) {
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		List<HoursenewsListVo> list = hourseNewDao.getHoursenewsListVos(uid, start, rows);
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		result.put(GlobConst.page_total, hourseNewDao.getHoursenewsListCount(uid));//total键 存放总记录数，必须的  
        return result;
    }
	
	public void deleteHoursernew(Long id) {
		hourseNewDao.deleteById(id);
	}
}
